package com.example.springboot.Service.impl;

import com.example.springboot.Service.ICategoryService;
import com.example.springboot.controller.request.BaseRequset;
import com.example.springboot.entity.Category;
import com.example.springboot.mapper.CategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@Service
public class CategoryService implements ICategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public List<Category> list() {
        return categoryMapper.list();
    }

    @Override
    public PageInfo<Category> page(BaseRequset baseRequset) {
        //分页依赖，只需要传入页数和页大小
        PageHelper.startPage(baseRequset.getPageNum(), baseRequset.getPageSize());
        //自关联查询，父子结构
        List<Category> categorys = categoryMapper.listByCondition(baseRequset);
        return new PageInfo<>(categorys);
    }

    @Override
    public void save(Category obj) {
        categoryMapper.save(obj);
    }

    @Override
    public Category getById(Integer id) {
        return  categoryMapper.getById(id);
    }

    @Override
    public void update(Category obj) {
        obj.setUpdatetime(LocalDate.now());
        categoryMapper.updateById(obj);
    }

    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteById(id);
    }
}
